package com.tuweiya.study.springmvc002.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: Mr_tu
 * @Date: Create in 17:42 2018/6/6
 * @Description: Swagger2 的配置类
 */
@Configuration      // 注入Bean 实例
@EnableWebMvc       // 非SpringBoot 项目要加此注解来启动
@EnableSwagger2     // 启动Swagger2 功能
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    /**
     * 创建Swagger2 功能
     */
    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.any())// 对所有api进行监
                .paths(PathSelectors.any())// 对所有路径进行监控
                .build();
    }

    /**
     * 设置Swagger2 的说明文档
     */
    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                .title("Spring MVC 002")
                .description("Spring MVC 002 的接口说明文档")
                .version("1.0")
                .build();

    }

    /**
     * 用来配置页面访问路径 和 swagger静态文件的加载路径
     *
     * @param registry 配置
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
